package com.liming.auth.service.impl;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.util.IdUtil;
import com.liming.auth.service.CaptchaService;
import com.liming.common.constant.RedisConstants;
import com.liming.common.pojo.vo.authinfo.CaptchaVO;
import com.liming.service.redis.RedisClient;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.util.concurrent.TimeUnit;

import static com.liming.common.constant.TimeConstants.CAPTCHA_EXPIRE_TIME;

/**
 * @author liming
 * @date 2025/1/13
 */
@Slf4j
@Service
@AllArgsConstructor
public class CaptchaServiceImpl implements CaptchaService {

    private final RedisClient redisClient;

    @Override
    public CaptchaVO lineCaptcha() {
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(600, 300);
        lineCaptcha.setFont(new Font("微软雅黑", Font.PLAIN, 50));
        CaptchaVO captchaVO = new CaptchaVO();
        captchaVO.setCaptchaId(IdUtil.fastSimpleUUID());
        captchaVO.setCaptchaImage(lineCaptcha.getImageBase64Data());
        // 将验证码ID存入Redis中
        redisClient.valueSet(RedisConstants.CAPTCHA_KEY + captchaVO.getCaptchaId(), lineCaptcha.getCode(), CAPTCHA_EXPIRE_TIME, TimeUnit.MILLISECONDS);
        return captchaVO;
    }
}
